package com.group.admin.MqListener;

import com.group.common.utils.MsgUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;


@Service
@Slf4j
@RabbitListener(queues = "groupuser")
public class LoginListener {
    @Autowired
    RedisTemplate redisTemplate;

    @RabbitHandler
    public void aa(String phone, Channel channel, Message message) throws IOException {
        try {
            //发送的验证码
            String s = MsgUtil.sendMsg(phone);
            log.info("手机号："+phone+"验证码："+s);
            redisTemplate.opsForValue().set(phone+"",s);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }
}
